package com.zmp.kotlinnote.view

import android.animation.Animator
import android.animation.ValueAnimator
import android.content.Context
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.util.AttributeSet
import android.util.Log
import android.view.View
import kotlin.math.abs

/**
 * @author zmp
 * Created by zmp on 2019/1/17
 * TODO
 */
class PointView @JvmOverloads constructor(context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0) :
    View(context, attrs, defStyleAttr) {

    val paint = Paint(Paint.ANTI_ALIAS_FLAG);
    var rd: Float = 0.0F
    var centerX: Float = 0.0F
    var centerY: Float = 0.0F
    var valueAnimator = ValueAnimator.ofFloat(0F, rd * 0.6F * 4)

    var animatedValue = 0F

    init {
        paint.color = Color.WHITE
        valueAnimator.duration = 1000

        valueAnimator.addUpdateListener {
            animatedValue = it.animatedValue as Float
            Log.e("animatedValue", "animatedValue:$animatedValue")
            postInvalidate()
        }
        valueAnimator.addListener(object : Animator.AnimatorListener {
            override fun onAnimationRepeat(animation: Animator?) {
            }

            override fun onAnimationEnd(animation: Animator?) {
                Log.e("onAnimationEnd", "animatedValue:$animatedValue")

                checkIndex()
                Log.e("onAnimationEnd", "animatedValue:$animatedValue" + index)
                postInvalidate()

            }

            override fun onAnimationCancel(animation: Animator?) {
            }

            override fun onAnimationStart(animation: Animator?) {
            }
        })
    }

    override fun onSizeChanged(w: Int, h: Int, oldw: Int, oldh: Int) {
        super.onSizeChanged(w, h, oldw, oldh)
        rd = measuredHeight / 2 * 0.6F / 2
        centerX = measuredWidth / 2F
        centerY = measuredHeight / 2F
        valueAnimator.setFloatValues(0F, rd * 0.6F * 4)
    }

    private var index = 0

    var maxCount = 10

    var selectCount = 0
    var isAdd = false

    fun checkIndex() {
        animatedValue = 0F
        start = 0
        end = 4
        index = when (selectCount) {
            0 -> 0
            1 -> 1
            maxCount -> 4
            maxCount - 1 -> 3
            maxCount - 2 -> 2
            else -> {
                2
            }
        }
    }

    fun next() {
        checkIndex()
        selectCount++
        if (selectCount > maxCount) {
            selectCount = maxCount
        }

        when (selectCount) {
            maxCount -> index = 4
            maxCount - 1 -> index = 3
            0 -> index = 0
            1 -> index = 1
            2 -> index = 2
            else -> {
                index++
//                end = 5
                isAdd = true
//                postDelayed({
//                    index--
//                    invalidate()
//                }, 1000)
                valueAnimator.start()
            }
        }
        invalidate()
    }

    fun up() {
        checkIndex()
        selectCount--
        if (selectCount < 0) {
            selectCount = 0
        }
        when (selectCount) {
            0 -> index = 0
            1 -> index = 1
            maxCount -> index = 4
            maxCount - 1 -> index = 3
            maxCount - 2 -> index = 2
            else -> {
                index--
                isAdd = false
//                start = -1
                valueAnimator.start()
//                postDelayed({
//                    index++
//                    invalidate()
//                }, 1000)
            }
        }

        invalidate()
    }

    var start = 0
    var end = 4
    override fun onDraw(canvas: Canvas?) {
        super.onDraw(canvas)

        canvas?.run {
            if (isAdd) {
                canvas.translate(-animatedValue, 0F)
            } else {
                canvas.translate(animatedValue, 0F)
            }
            paint.color = Color.RED
            var cR: Float
            for (i in start..end) {
                if (i == index) {
                    cR = rd
                    paint.color = Color.RED
                } else {
                    cR = rd * 0.8F
                    paint.color = Color.WHITE
                }
                drawCircle(centerX + 4 * (i - 2) * rd * 0.6F, centerY, cR, paint)
            }
        }

    }
}
